#include "MyCCFollow.h"

MyCCFollow* MyCCFollow::create(CCNode *pFollowedNode, float screen_per, const CCRect& rect/* = CCRectZero*/)
{
	MyCCFollow *pRet = new MyCCFollow();
	pRet->screen_per = screen_per;
	if (pRet && pRet->initWithTarget(pFollowedNode, rect))
	{
		pRet->autorelease();
		return pRet;
	}
	CC_SAFE_DELETE(pRet);
	return NULL;
}
bool MyCCFollow::initWithTarget(CCNode *pFollowedNode, const CCRect& rect /* = CCRectZero */)
{
	CCAssert(pFollowedNode != NULL, "");

	pFollowedNode->retain();
	m_pobFollowedNode = pFollowedNode;
	if (rect.equals(CCRectZero))
	{
		m_bBoundarySet = false;
	}
	else
	{
		m_bBoundarySet = true;
	}

	m_bBoundaryFullyCovered = false;

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	m_obFullScreenSize = CCPointMake(winSize.width, winSize.height);
	m_obHalfScreenSize = ccpMult(m_obFullScreenSize, screen_per);

	if (m_bBoundarySet)
	{
		m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x);
		m_fRightBoundary = -rect.origin.x ;
		m_fTopBoundary = -rect.origin.y;
		m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y);

		if(m_fRightBoundary < m_fLeftBoundary)
		{
			// screen width is larger than world's boundary width
			//set both in the middle of the world
			m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary) * screen_per;
		}
		if(m_fTopBoundary < m_fBottomBoundary)
		{
			// screen width is larger than world's boundary width
			//set both in the middle of the world
			m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary) / 2;
		}

		if( (m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary) )
		{
			m_bBoundaryFullyCovered = true;
		}
	}

	return true;
}
